package io.bluerain.ldrawersample.entities;

import android.content.Context;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by foredawn on 15-9-23.
 * SimpleAdapter实现的listView构造类
 */
public class SimpleMenu {

    public static SimpleMenu builder() {
        return new SimpleMenu();
    }

    private List<Map<String, ?>> items = new ArrayList<>();

    public SimpleMenu setItems(Menu[] menus) {
        if (menus == null || menus.length == 0)
            return null;
        Map<String, Object> item = null;
        for (int i = 0; i < menus.length; i++) {
            item = new HashMap<>();
            item.put(Menu.KER_IMG, menus[i].getImgId()); //设置图片
            item.put(Menu.KEY_TEXT, menus[i].getText()); //设置文字
            items.add(item);
        }
        return this;
    }

    public SimpleAdapter genAdapter(Context context, int resource, int[] toViews) {
        SimpleAdapter adapter = new SimpleAdapter(context, items, resource,
                Menu.KEYS,
                toViews);
        return adapter;
    }
}
